package com.kzj.global.utils;

import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.exception.MyException;
import io.swagger.models.auth.In;
import jodd.util.StringUtil;
import org.springframework.stereotype.Component;

import javax.xml.bind.annotation.XmlElementDecl;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GlobalUtil {
    public static Integer getStudentTermByDateOfEnrollment(LocalDate date)
    {

        LocalDate now = LocalDate.now();
        Integer term=0;
        for(;!date.isAfter(now);date=date.plusMonths(6))
        {
            term++;
        }
        return term;
    }
    public static String getStringTerm(String stringTerm)
    {
        String[] split = stringTerm.split(GlobalConstant.CURRICULUM_TIME_SPLIT);
        String tag=split[1].equals(GlobalConstant.UPPER_TERM_FLAG)?"上":"下";
        return split[0]+"年 "+tag+"学期";
    }
    public static String valueRemoveId(String value,String id)
    {
        String[] split = value.split(",");
        List<String> list=new ArrayList<>();
        for (String s : split) {
            if(!s.equals(id))
                list.add(s);
        }
        return StringUtil.join(list,",");
    }
    public static String getHandlerList(List<String> list)
    {
        list=list.stream().map(item->"'"+item+"'").collect(Collectors.toList());
        return StringUtil.join(list,",");
    }
    public static LocalDate getDateByWeekAndDay(Integer day,Integer week)
    {
        int amount=(week-1)*7+day;
        return getCurrOpeningDate().plusDays(amount);
    }
    public static LocalDateTime getDateTimeByWeekAndDayAndTime(Integer day,Integer week,Integer time)
    {
        LocalDate dateByWeekAndDay = getDateByWeekAndDay(day, week);
        return LocalDateTime.of(dateByWeekAndDay, LocalTime.of(8+time*2,40));
    }
    public static LocalDate getCurrOpeningDate()
    {
        LocalDate now = LocalDate.now();
        return getOpeningDate(now.getYear(),now.getMonthValue());
    }
    public static LocalDate getOpeningDate(Integer year,Integer month)
    {
        LocalDate openingDate=null;
        
        if(month< GlobalConstant.UPPER_TERM_MONTH)
        {
            LocalDate localDate = LocalDate.of(year, GlobalConstant.DOWN_TERM_MONTH, 1);
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            int value = dayOfWeek.getValue();
            if(value!=1)
            {
                 localDate = localDate.plusDays(7 - value + 1);
            }
            openingDate=localDate;

        }
        else {
            LocalDate localDate = LocalDate.of(year, GlobalConstant.UPPER_TERM_MONTH, 1);
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            int value = dayOfWeek.getValue();
            if(value!=1)
            {
                localDate = localDate.plusDays(7 - value + 1);
            }
            openingDate=localDate;
        }
        return openingDate;
    }
    public static String getCronByDate(LocalDateTime dateTime)
    {
        int year = dateTime.getYear();
        int month = dateTime.getMonthValue();
        int dayOfMonth = dateTime.getDayOfMonth();
        int hour = dateTime.getHour();
        int minute = dateTime.getMinute();
        int second=dateTime.getSecond();
        String cronExpression = String.format("%d %d %d %d %d ?",second, minute, hour, dayOfMonth, month);
        return cronExpression;
    }
    public static String getCurrentTerm()
    {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int monthValue = now.getMonthValue();
        if(monthValue>GlobalConstant.DOWN_TERM_MONTH&&monthValue<GlobalConstant.UPPER_TERM_MONTH)
        {
            return year+GlobalConstant.COURSE_SCHOOL_TIME_SPLIT_JOIN+GlobalConstant.DOWN_TERM_FLAG;
        }
        else{
            if(monthValue<GlobalConstant.DOWN_TERM_MONTH)
                return (year-1)+GlobalConstant.COURSE_SCHOOL_TIME_SPLIT_JOIN+GlobalConstant.UPPER_TERM_FLAG;
            else return year+GlobalConstant.COURSE_SCHOOL_TIME_SPLIT_JOIN+GlobalConstant.UPPER_TERM_FLAG;

        }
    }
    public static String getNextTerm()
    {
        return getTagTerm(1);
    }
    public static String getTagTerm(Integer val)
    {
        String[] split = getCurrentTerm().split(GlobalConstant.CURRICULUM_TIME_SPLIT);
        Integer year=Integer.valueOf(split[0]);
        String termFlag=split[1];
        if(val<0)
        {
            year-=val/2;
            if(val%2!=0)
            {
                if(termFlag.equals(GlobalConstant.DOWN_TERM_FLAG))
                {
                    year--;
                    termFlag=GlobalConstant.UPPER_TERM_FLAG;
                }
                else{
                    termFlag=GlobalConstant.DOWN_TERM_FLAG;
                }
            }
        }
        else{
            year+=val/2;
            if(val%2!=0)
            {
                if(termFlag.equals(GlobalConstant.UPPER_TERM_FLAG))
                {
                    year++;
                    termFlag=GlobalConstant.DOWN_TERM_FLAG;
                }
                else{
                    termFlag=GlobalConstant.UPPER_TERM_FLAG;
                }
            }
        }
        return year+GlobalConstant.CURRICULUM_TIME_SPLIT_JOIN+termFlag;
    }
    public static String addZero(Integer num, Integer len)
    {
        String str=num+"";
        if(str.length()>len)
            throw new MyException(GlobalConstant.NUMBER_OVER);
        while(str.length()<len)
        {
            str="0"+str;
        }
        return str;
    }
}
